<?php

namespace app\api\controller\shop;


use app\admin\model\device\Air;
use app\admin\model\Shopbalanceprofit;
use app\admin\model\Shopcharge;
use app\admin\model\Shopdevice;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoptableorder;
use app\admin\model\Supplier;
use app\admin\model\Tuanorder;
use app\admin\model\Userbalance;
use app\common\controller\Api;

/**
 *
 */
class Index extends Api
{
    protected $noNeedLogin = ['addSupplier'];
    protected $noNeedRight = ['*'];

    /**
     * 首页数据
     *
     */
    public function index()
    {


        $shop_id = $this->request->param('shop_id');
        $date = $this->request->param('date');
        $shopOrderModel = new Shoporder();
        $where = array();
        $where['shop_id'] = $shop_id;
        $where['status'] = array('not in', ['paying', 'refund']);
        $shopOrderProfitModel = new Shoporderprofit();
        $chargeOrderModel = new Shopcharge();
        $tuanOrderModel = new Tuanorder();
        if ($date == 1) {
            //今天
            $bookAmount = $shopOrderModel->whereTime("create_time", 'today')->where($where)->sum('amount');
            $bookOrder = $shopOrderModel->whereTime("create_time", 'today')->where($where)->count('id');

            $tuanAmount = $tuanOrderModel->whereTime("create_time", 'today')->where($where)->sum('amount');
            $tuanOrder = $tuanOrderModel->whereTime("create_time", 'today')->where($where)->count('id');


            $chargeAmount = $chargeOrderModel->whereTime("create_time", 'today')->where($where)->sum('amount');
            $chargeOrder = $chargeOrderModel->whereTime("create_time", 'today')->where($where)->count('id');

            $totalAmount = $bookAmount + $tuanAmount + $chargeAmount;
            $totalOrder = $bookOrder + $tuanOrder + $chargeOrder;
            $profit = $shopOrderProfitModel->whereTime('create_time', 'today')->where('shop_id', $shop_id)->sum('amount');
        } else if ($date == 2) {
            //昨天
            $bookAmount = $shopOrderModel->whereTime("create_time", 'yesterday')->where($where)->sum('amount');
            $bookOrder = $shopOrderModel->whereTime("create_time", 'yesterday')->where($where)->count('id');
            $profit = $shopOrderProfitModel->whereTime('create_time', 'yesterday')->where('shop_id', $shop_id)->sum('amount');

            $tuanAmount = $tuanOrderModel->whereTime("create_time", 'yesterday')->where($where)->sum('amount');
            $tuanOrder = $tuanOrderModel->whereTime("create_time", 'yesterday')->where($where)->count('id');


            $chargeAmount = $chargeOrderModel->whereTime("create_time", 'yesterday')->where($where)->sum('amount');
            $chargeOrder = $chargeOrderModel->whereTime("create_time", 'yesterday')->where($where)->count('id');

            $totalAmount = $bookAmount + $tuanAmount + $chargeAmount;
            $totalOrder = $bookOrder + $tuanOrder + $chargeOrder;

        } else {
            //本月
            $bookAmount = $shopOrderModel->whereTime("create_time", 'month')->where($where)->sum('amount');
            $bookOrder = $shopOrderModel->whereTime("create_time", 'month')->where($where)->count('id');
            $profit = $shopOrderProfitModel->whereTime('create_time', 'month')->where('shop_id', $shop_id)->sum('amount');


            $tuanAmount = $tuanOrderModel->whereTime("create_time", 'month')->where($where)->sum('amount');
            $tuanOrder = $tuanOrderModel->whereTime("create_time", 'month')->where($where)->count('id');


            $chargeAmount = $chargeOrderModel->whereTime("create_time", 'month')->where($where)->sum('amount');
            $chargeOrder = $chargeOrderModel->whereTime("create_time", 'month')->where($where)->count('id');


            $totalAmount = $bookAmount + $tuanAmount + $chargeAmount;
            $totalOrder = $bookOrder + $tuanOrder + $chargeOrder;
        }
        $ret = array();
        $ret['totalAmount'] = formatMoney($totalAmount);
        $ret['totalOrder'] = $totalOrder;
        $ret['totalProfit'] = formatMoney($profit);
        $ret['checkAmount'] = 0;


        $this->success('请求成功', $ret);
    }

    public function v2Index()
    {


        $shop_id = $this->request->param('shop_id');
        $date = $this->request->param('date');

        //查询商户抽佣类型方便前端判断是否需要开启退款功能
        $shopModel = new \app\admin\model\Shop();
        $shopInfo = $shopModel->where('id',$shop_id)->find();
        if(empty($shopInfo)) $this->error('店铺不存在');

        $shopOrderModel = new Shoporder();
        $where = array();
        $where['shop_id'] = $shop_id;
        $where['status'] = array('not in', ['paying', 'refund']);
        $shopOrderProfitModel = new Shoporderprofit();
        $chargeOrderModel = new Shopcharge();
        $tuanOrderModel = new Tuanorder();
        $tqTableOrderModel = new Shoptableorder();
        $userBalanceModel = new Userbalance();
        $shopDeviceModel = new Shopdevice();
        $shopBallanceModel = new Shopbalanceprofit();

        $tqAmount = 0;
        if ($date == 1) {
            //今天
            $bookAmount = $shopOrderModel->whereTime("create_time", 'today')->where($where)->sum('amount');
            $bookOrder = $shopOrderModel->whereTime("create_time", 'today')->where($where)->count('id');

            $tuanAmount = $tuanOrderModel->whereTime("create_time", 'today')->where($where)->sum('amount');
            $tuanOrder = $tuanOrderModel->whereTime("create_time", 'today')->where($where)->count('id');

            $tuanCheckTotalAmount = $shopOrderModel->whereTime("create_time", 'today')->where('tuan_id', "<>", -1)->where($where)->sum('amount');
            $tuanCheckPayAmount = $shopOrderModel->whereTime("create_time", 'today')->where('tuan_id', "<>", -1)->where($where)->sum('pay_amount');

            $tuanCheckAmount = $tuanCheckTotalAmount - $tuanCheckPayAmount;

            $balanceAmount1 = $shopOrderModel->whereTime("create_time", 'today')->where($where)->sum('balance');

            $balanceAmount2 = $tqTableOrderModel->whereTime("book_end", 'today')->where('shop_id', $shop_id)->where("balance", "<>", 0)->sum('balance');

            $balanceAmount = $balanceAmount1 + $balanceAmount2;

            $chargeAmount = $chargeOrderModel->whereTime("create_time", 'today')->where($where)->sum('amount');
            $chargeOrder = $chargeOrderModel->whereTime("create_time", 'today')->where($where)->count('id');


            $tqAmount = $tqTableOrderModel->whereTime("book_end", 'today')->where('shop_id', $shop_id)->where('status', 'done')->sum('amount');
            $tqOrder = $tqTableOrderModel->whereTime("book_end", 'today')->where('shop_id', $shop_id)->where('status', 'in', 'done')->count('id');

            $totalAmount = $bookAmount + $tuanAmount + $chargeAmount + $tqAmount;
            $totalOrder = $bookOrder + $tuanOrder + $chargeOrder + $tqOrder;


            $mtCheckTotalAmount = $shopOrderModel->whereTime("create_time", 'today')->where('mt_code', "<>", "")->where($where)->sum('amount');
            $mtCheckPayAmount = $shopOrderModel->whereTime("create_time", 'today')->where('mt_code', "<>", "")->where($where)->sum('pay_amount');


            $mtCheckTotalAmount2 = $tqTableOrderModel->whereTime("book_end", 'today')->where('mt_code', "<>", "")->where($where)->sum('amount');
            $douCheckTotalAmount2 = $tqTableOrderModel->whereTime("book_end", 'today')->where('dou_code', "<>", "")->where($where)->sum('amount');


            $douCheckTotalAmount = $shopOrderModel->whereTime("create_time", 'today')->where('dou_code', "<>", "")->where($where)->sum('amount');
            $douCheckPayAmount = $shopOrderModel->whereTime("create_time", 'today')->where('dou_code', "<>", "")->where($where)->sum('pay_amount');


            $mtAmount = $mtCheckTotalAmount - $mtCheckPayAmount + $mtCheckTotalAmount2;
            $douAmount = $douCheckTotalAmount - $douCheckPayAmount + $douCheckTotalAmount2;

            $profit = $shopOrderProfitModel->whereTime('create_time', 'today')->where('shop_id', $shop_id)->sum('amount');
            $balanceProfit = $shopBallanceModel->whereTime('create_time', 'today')->where('shop_id', $shop_id)->sum('amount');

        } else if ($date == 2) {
            //昨天
            $bookAmount = $shopOrderModel->whereTime("create_time", 'yesterday')->where($where)->sum('amount');
            $bookOrder = $shopOrderModel->whereTime("create_time", 'yesterday')->where($where)->count('id');
            $profit = $shopOrderProfitModel->whereTime('create_time', 'yesterday')->where('shop_id', $shop_id)->sum('amount');

            $tuanAmount = $tuanOrderModel->whereTime("create_time", 'yesterday')->where($where)->sum('amount');
            $tuanOrder = $tuanOrderModel->whereTime("create_time", 'yesterday')->where($where)->count('id');

            $balanceAmount1 = $shopOrderModel->whereTime("create_time", 'yesterday')->where($where)->sum('balance');


            $balanceAmount2 = $tqTableOrderModel->whereTime("book_end", 'yesterday')->where('shop_id', $shop_id)->where("balance", "<>", 0)->sum('balance');
            $balanceAmount = $balanceAmount1 + $balanceAmount2;

            $chargeAmount = $chargeOrderModel->whereTime("create_time", 'yesterday')->where($where)->sum('amount');
            $chargeOrder = $chargeOrderModel->whereTime("create_time", 'yesterday')->where($where)->count('id');

            $tqAmount = $tqTableOrderModel->whereTime("book_end", 'yesterday')->where('shop_id', $shop_id)->where('status', 'done')->sum('amount');
            $tqOrder = $tqTableOrderModel->whereTime("book_end", 'yesterday')->where('shop_id', $shop_id)->where('status', 'in', 'done')->count('id');


            $totalAmount = $bookAmount + $tuanAmount + $chargeAmount + $tqAmount;
            $totalOrder = $bookOrder + $tuanOrder + $chargeOrder + $tqOrder;

            $tuanCheckTotalAmount = $shopOrderModel->whereTime("create_time", 'yesterday')->where('tuan_id', "<>", -1)->where($where)->sum('amount');
            $tuanCheckPayAmount = $shopOrderModel->whereTime("create_time", 'yesterday')->where('tuan_id', "<>", -1)->where($where)->sum('pay_amount');

            $tuanCheckAmount = $tuanCheckTotalAmount - $tuanCheckPayAmount;


            $mtCheckTotalAmount = $shopOrderModel->whereTime("create_time", 'yesterday')->where('mt_code', "<>", "")->where($where)->sum('amount');
            $mtCheckPayAmount = $shopOrderModel->whereTime("create_time", 'yesterday')->where('mt_code', "<>", "")->where($where)->sum('pay_amount');


            $douCheckTotalAmount = $shopOrderModel->whereTime("create_time", 'yesterday')->where('dou_code', "<>", "")->where($where)->sum('amount');
            $douCheckPayAmount = $shopOrderModel->whereTime("create_time", 'yesterday')->where('dou_code', "<>", "")->where($where)->sum('pay_amount');


            $mtCheckTotalAmount2 = $tqTableOrderModel->whereTime("book_end", 'yesterday')->where('mt_code', "<>", "")->where($where)->sum('amount');
            $douCheckTotalAmount2 = $tqTableOrderModel->whereTime("book_end", 'yesterday')->where('dou_code', "<>", "")->where($where)->sum('amount');


            $mtAmount = $mtCheckTotalAmount - $mtCheckPayAmount + $mtCheckTotalAmount2;
            $douAmount = $douCheckTotalAmount - $douCheckPayAmount + $douCheckTotalAmount2;
            $balanceProfit = $shopBallanceModel->whereTime('create_time', 'yesterday')->where('shop_id', $shop_id)->sum('amount');

        } else {
            //本月
            $bookAmount = $shopOrderModel->whereTime("create_time", 'month')->where($where)->sum('amount');
            $bookOrder = $shopOrderModel->whereTime("create_time", 'month')->where($where)->count('id');
            $profit = $shopOrderProfitModel->whereTime('create_time', 'month')->where('shop_id', $shop_id)->sum('amount');


            $tuanAmount = $tuanOrderModel->whereTime("create_time", 'month')->where($where)->sum('amount');
            $tuanOrder = $tuanOrderModel->whereTime("create_time", 'month')->where($where)->count('id');

            $balanceAmount1 = $shopOrderModel->whereTime("create_time", 'month')->where($where)->sum('balance');
            $balanceAmount2 = $tqTableOrderModel->whereTime("book_end", 'month')->where('shop_id', $shop_id)->where("balance", "<>", 0)->sum('balance');
            $balanceAmount = $balanceAmount1 + $balanceAmount2;

            $chargeAmount = $chargeOrderModel->whereTime("create_time", 'month')->where($where)->sum('amount');
            $chargeOrder = $chargeOrderModel->whereTime("create_time", 'month')->where($where)->count('id');

            $tqAmount = $tqTableOrderModel->whereTime("book_end", 'month')->where('shop_id', $shop_id)->where('status', 'done')->sum('amount');
            $tqOrder = $tqTableOrderModel->whereTime("book_end", 'month')->where('shop_id', $shop_id)->where('status', 'in', 'done')->count('id');


            $totalAmount = $bookAmount + $tuanAmount + $chargeAmount + $tqAmount;
            $totalOrder = $bookOrder + $tuanOrder + $chargeOrder + $tqOrder;


            $tuanCheckTotalAmount = $shopOrderModel->whereTime("create_time", 'month')->where('tuan_id', "<>", -1)->where($where)->sum('amount');
            $tuanCheckPayAmount = $shopOrderModel->whereTime("create_time", 'month')->where('tuan_id', "<>", -1)->where($where)->sum('pay_amount');

            $tuanCheckAmount = $tuanCheckTotalAmount - $tuanCheckPayAmount;

            $mtCheckTotalAmount = $shopOrderModel->whereTime("create_time", 'month')->where('mt_code', "<>", "")->where($where)->sum('amount');
            $mtCheckPayAmount = $shopOrderModel->whereTime("create_time", 'month')->where('mt_code', "<>", "")->where($where)->sum('pay_amount');


            $douCheckTotalAmount = $shopOrderModel->whereTime("create_time", 'month')->where('dou_code', "<>", "")->where($where)->sum('amount');
            $douCheckPayAmount = $shopOrderModel->whereTime("create_time", 'month')->where('dou_code', "<>", "")->where($where)->sum('pay_amount');


            $mtCheckTotalAmount2 = $tqTableOrderModel->whereTime("book_end", 'month')->where('mt_code', "<>", "")->where($where)->sum('amount');
            $douCheckTotalAmount2 = $tqTableOrderModel->whereTime("book_end", 'month')->where('dou_code', "<>", "")->where($where)->sum('amount');


            $mtAmount = $mtCheckTotalAmount - $mtCheckPayAmount + $mtCheckTotalAmount2;
            $douAmount = $douCheckTotalAmount - $douCheckPayAmount + $douCheckTotalAmount2;

            $balanceProfit = $shopBallanceModel->whereTime('create_time', 'month')->where('shop_id', $shop_id)->sum('amount');

        }
        $ret = array();
        $ret['profit_type'] = $shopInfo['profit_type'];
        $ret['totalAmount'] = formatMoney($totalAmount);
        $ret['totalOrder'] = $totalOrder;
        $ret['mdOrder'] = $bookOrder;

        $ret['totalProfit'] = formatMoney($profit + $balanceProfit);
        $ret['checkAmount'] = formatMoney($mtAmount + $douAmount);
        $ret['tuanCheckAmount'] = formatMoney($tuanCheckAmount);
        $ret['balanceAmount'] = formatMoney($balanceAmount);
        $ret['bookAmount'] = formatMoney($bookAmount);
        $ret['tuanAmount'] = formatMoney($tuanAmount);
        $ret['chargeAmount'] = formatMoney($chargeAmount);
        $ret['mtAmount'] = formatMoney($mtAmount);
        $ret['douAmount'] = formatMoney($douAmount);
        $ret['taiAmount'] = formatMoney($tqAmount);
        $ret['air'] = -1;
        $device = $shopDeviceModel->where('shop_id', $shop_id)->where('type', 'air')->find();
        if ($device) {
            $air = 1;
            $ret['air'] = $air;
        }

        $airModel = new Air();
        $airData = $airModel->where('shop_id', $shop_id)->find();
        if ($airData) {
            $ret['air'] = 2;
        }

        $this->success('请求成功', $ret);
    }


    public function addSupplier()
    {

        $city = $this->request->param('city');
        $name = $this->request->param('name');
        $mobile = $this->request->param('mobile');
        $company = $this->request->param('company');
        $isDing = $this->request->param('isDing');
        $isService = $this->request->param('isService');
        $roleName = $this->request->param('roleName');
        $type = $this->request->param('type');
        $price = $this->request->param('price');

        $db = array();
        $db['role_name'] = $roleName;
        $db['mobile'] = $mobile;
        $db['name'] = $name;
        $db['type'] = $type;
        $db['price'] = $price;

        $db['city'] = $city;
        $db['company_name'] = $company;
        $db['ding'] = $isDing;
        $db['service'] = $isService;

        $supplierModel = new Supplier();
        $supplierModel->insert($db);

        $this->success('');

    }
}
